package gob.cgr.web.sgrh.util;

import gob.cgr.sgrh.ejb.entidad.MovimientoDePersonal;
import gob.cgr.sgrh.ejb.entidad.Personal;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;

import org.primefaces.context.RequestContext;




public class Utilitario {

	public static String HOST_LEGAJO="http://sigerh.byethost7.com";
	public static boolean isNullOrEmpty(String s) {
		if (s == null)
			return true;
		if (s.trim().length() == 0)
			return true;
		return false;

	}

	public static String getCarpetaFoto() {
		String str = System.getenv("OPENSHIFT_DATA_DIR");

		if (str == null) {
			str = "D:/datajboss";
		}

		return str + "/upload/";
	}

	public static String getCarpetaLegajo() {
		String str = System.getenv("OPENSHIFT_DATA_DIR");

		if (str == null) {
			str = "D:/datajboss";
		}

		return str + "/legajos/";
	}
	
	public static String getCarpetaTemplateDocs() {
		String str = System.getenv("OPENSHIFT_DATA_DIR");

		if (str == null) {
			str = "D:/datajboss";
		}

		return str + "/TEMPLATE_DIRECTORY/";
	}

	public static void mostrarMensajeAlerta(String msj) {
		RequestContext.getCurrentInstance().addCallbackParam("notValid", true);

		FacesContext.getCurrentInstance().addMessage(null,
				new FacesMessage(FacesMessage.SEVERITY_WARN, "Alerta", msj));
	}

	public static void mostrarMensajeExito(String msj) {
		FacesContext.getCurrentInstance().addMessage(
				null,
				new FacesMessage(FacesMessage.SEVERITY_INFO, "Confirmacion OK",
						msj));
	}

	public static String izquierda(String s, int caracteres) {
		// System.out.println("izquierda recibiendo :"+s);
		if (s == null)
			return null;
		if (s.length() > caracteres) {
			// System.out.println("quitando caracteres: "+caracteres);
			return s.substring(s.length() - caracteres, s.length());
		} else {
			return s;
		}
	}

	public static Flash getFlashScope() {
		return (FacesContext.getCurrentInstance().getExternalContext()
				.getFlash());
	}
	
	
	
	
	public static Map<String, String> getMapByTemplate(String template,Personal personal){
		Map<String, String> m=new HashMap<String, String>();
		
		if(template.equals("CONSTANCIA DE TRABAJO")){
			MovimientoDePersonal mov=personal.getMovimientoDePersonalList().get(0);
			Calendar c=Calendar.getInstance(new Locale("es","ES"));
			
			m.put(":personal.unidadOrganica", mov.getIdUnidadOrganica().getNombreUnidad());
			m.put(":personal.nombreCompleto", personal.getIdPersona().getNombreCompleto());
			m.put(":personal.fechaIngreso", String.format("%1$td-%1$tm-%1$tY", personal.getFechaIngreso()));
			m.put(":personal.cargo", mov.getIdPuesto().getNombre());
			m.put(":dia",c.get(Calendar.DATE)+"");
			
			m.put(":mes",String.format(new Locale("es","PE"),"%1$tB", c));
			m.put(":anio",c.get(Calendar.YEAR)+"");
			m.put(":siglaUO",mov.getIdUnidadOrganica().getSiglas());
		}
		
		return m;
		
	}
	

	/*public static void redirect( String mappingId,
			Map<String, String[]>... params) {
		FacesContext ctx=FacesContext.getCurrentInstance();
		HttpServletRequest request=(HttpServletRequest)ctx.getExternalContext().getRequest();
		HttpServletResponse response=(HttpServletResponse)ctx.getExternalContext().getResponse();
		
		//params=params==null?new HashMap<String, String[]>():params;
		
		PrettyContext context = PrettyContext.getCurrentInstance(request);
		PrettyURLBuilder builder = new PrettyURLBuilder();

		UrlMapping mapping = context.getConfig().getMappingById(mappingId);
		String targetURL = builder.build(mapping, params);

		targetURL = request.getContextPath()+ response.encodeRedirectURL(targetURL);
		System.out.println("Faces el target es "+targetURL);
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect(targetURL);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("error al redireccionar desde util");
			e.printStackTrace();
		}
		
	}
*/
}
